//
//  JCDetailsViewController.m
//  ABookOfCartoon
//
//  Created by dlios on 15-5-18.
//  Copyright (c) 2015年 Power Of Ants. All rights reserved.
//

#import "JCDetailsViewController.h"
#import "UIImageView+WebCache.h"
#import "JCReadViewController.h"
#import "JCDataCollectManager.h"
#import "JCMainPageViewController.h"

@interface JCDetailsViewController ()<UIScrollViewDelegate
>
@property(nonatomic,retain)UIButton *button1;
@end

@implementation JCDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
     self.navigationController.navigationBarHidden = NO;
    self.title = self.str;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回主页面" style:UIBarButtonItemStylePlain target:self action:@selector(left)];
    
    UIScrollView *scollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.bounds.size.height)];
    scollView.contentSize = CGSizeMake(0, 1000);
    scollView.delegate = self;
    [self.view addSubview:scollView];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, self.view.bounds.size.width / 3, self.view.bounds.size.width / 3 + 50)];
    imageView.backgroundColor = [UIColor redColor];
    [imageView sd_setImageWithURL:[NSURL URLWithString:_str1]];
    [scollView addSubview:imageView];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(190, 30, 200, 30)];
    label1.text = self.str2;
    [scollView addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(190, 60, 200, 30)];
    label2.text = self.str3;
    [scollView addSubview:label2];
    
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(190, 90, 200, 30)];
    label3.text = self.str4;
    [scollView addSubview:label3];
    
    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(150, 30, 40, 30)];
    label4.text = @"书名";
    [scollView addSubview:label4];
    
    
    UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(150, 60, 40, 30)];
    label5.text = @"作者";
    [scollView addSubview:label5];
    
    UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(150, 90, 40, 30)];
    label6.text = @"人气";
    [scollView addSubview:label6];
    
    UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(20, 220, self.view.frame.size.width - 60 , 300)];
    label7.text = self.str5;
    label7.numberOfLines = 0;
    [label7 sizeToFit];
    [scollView addSubview:label7];
    
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3, 180 + label7.frame.size.height + 40, self.view.frame.size.width / 3, 40)];
    [button setTitle:@"开始阅读" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button.layer setCornerRadius:5];
    [button.layer setBorderWidth:1];
 
    [button addTarget:self action:@selector(but) forControlEvents:UIControlEventTouchUpInside];
    [scollView addSubview:button];
    
    
    self.button1 = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3, 180 + label7.frame.size.height + 40 + button.frame.size.height , self.view.frame.size.width / 3, 40)];
    [_button1 setTitle:@"收藏" forState:UIControlStateNormal];
    [_button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_button1 addTarget:self action:@selector(but1) forControlEvents:UIControlEventTouchUpInside];
    [_button1.layer setCornerRadius:5];
    [_button1.layer setBorderWidth:1];
    [scollView addSubview:_button1];
    
   
    scollView.contentSize = CGSizeMake(0, 340 + label7.frame.size.height + _button1.frame.size.height + button.frame.size.height);


}
-(void)left
{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

-(void)but1
{
    JCDataCollectManager *dbManager = [JCDataCollectManager shareInstance];
    NSMutableArray *arr = [dbManager selectInfo];
    NSInteger aa = 0;
    for (JCMainPageModel *recommend in arr) {
        if ([recommend.BookName isEqualToString:[self.title stringByReplacingOccurrencesOfString:@"'" withString:@" "]]) {
            aa = 1;
        }
    }
       if (aa == 0)
{
        //插入数据
     JCDataCollectManager *dbManager = [JCDataCollectManager shareInstance];
            NSMutableArray *arr = [dbManager selectInfo];
            NSLog(@"%@",arr);
            [dbManager openDB];
            [dbManager createTable];
            JCMainPageModel *name = [[JCMainPageModel alloc]init];
            name.BookName = self.str2;
            [dbManager insertInfowithJCMainPageModel:name];
            [name release];
    UIAlertView *al1 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [al1 show];
            [al1 release];
        }else{
            UIAlertView *al2 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已被收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [al2 show];
            [al2 release];
        }
        
    }

-(void)but
{    
    JCReadViewController *read = [[JCReadViewController alloc]init];
    read.str7 = _str6;
    NSLog(@"~~%@",read.str7);
    [self.navigationController pushViewController:read animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
